package com.xiao.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoliang on 2016/5/23.
 * 2016/5/23 10:41
 *
 * @Version 1.0
 */
public class HtmlTemplateUtil {

    private static final String TITLES_SPLIT = "$_$titles$_$";
    private static final String CONTENT_SPLIT = "$_$contents$_$";
    private static String html ;
    static {
        html = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><title>预警邮件</title></head><style>body{margin:40px auto;font-family:Arial,helvetica,\"Microsoft Yahei\",sans-serif;font-size:14px;color:#444}table{*border-collapse:collapse;border-spacing:0;width:100%}.bordered{border:solid#ccc 1px;-moz-border-radius:6px;-webkit-border-radius:6px;border-radius:6px;-webkit-box-shadow:0 1px 1px#ccc;-moz-box-shadow:0 1px 1px#ccc;box-shadow:0 1px 1px#ccc}.bordered tr:hover{background:#fbf8e9;-o-transition:all 0.1s ease-in-out;-webkit-transition:all 0.1s ease-in-out;-moz-transition:all 0.1s ease-in-out;-ms-transition:all 0.1s ease-in-out;transition:all 0.1s ease-in-out}.bordered td,.bordered th{border-left:1px solid#ccc;border-top:1px solid#ccc;padding:10px;text-align:center}.bordered th{background-color:#dce9f9;background-image:-webkit-gradient(linear,left top,left bottom,from(#ebf3fc),to(#dce9f9));background-image:-webkit-linear-gradient(top,#ebf3fc,#dce9f9);background-image:-moz-linear-gradient(top,#ebf3fc,#dce9f9);background-image:-ms-linear-gradient(top,#ebf3fc,#dce9f9);background-image:-o-linear-gradient(top,#ebf3fc,#dce9f9);background-image:linear-gradient(top,#ebf3fc,#dce9f9);-webkit-box-shadow:0 1px 0 rgba(255,255,255,.8)inset;-moz-box-shadow:0 1px 0 rgba(255,255,255,.8)inset;box-shadow:0 1px 0 rgba(255,255,255,.8)inset;border-top:none;text-shadow:0 1px 0 rgba(255,255,255,.5)}</style><body><h2>预警邮件</h2><table class=\"bordered\"><thead><tr><th></th>$_$titles$_$</tr></thead>$_$contents$_$</table></body></html>";
    }

    /**
     * 转换模板
     *
     * @param templateSourceFile 模板文件绝对路径
     * @param titles             标题
     * @param paramDataList      标题参数
     * @param dataList           数据
     * @return 转换后的html串
     * @throws IOException
     */
    public static String dataToHtml(String templateSourceFile, List<String> titles,
                                    List<String> paramDataList, List<Map<String, String>> dataList) throws IOException {

        InputStreamReader read = null;
        read = new InputStreamReader(new FileInputStream(templateSourceFile), "utf-8");
        BufferedReader bufferedReader = new BufferedReader(read);
        String line = null;

        StringBuilder stringBuilder = new StringBuilder();

        while ((line = bufferedReader.readLine()) != null) {
            if (TITLES_SPLIT.equalsIgnoreCase(line.trim())) {
                stringBuilder.append(HtmlTemplateUtil.processTitles(titles));
            }else if (CONTENT_SPLIT.equalsIgnoreCase(line.trim())) {
                stringBuilder.append(HtmlTemplateUtil.processContents(paramDataList, dataList));
            }else {
                stringBuilder.append(line);
            }
        }

        read.close();
        return stringBuilder.toString();
    }

    /**
     * 转换模板
     * @param titles             标题
     * @param paramDataList      标题参数
     * @param dataList           数据
     * @return 转换后的html串
     * @throws IOException
     */
    public static String dataToHtml(List<String> titles,
                                    List<String> paramDataList, List<Map<String, String>> dataList) throws IOException {
        String title = html.replace(TITLES_SPLIT,HtmlTemplateUtil.processTitles(titles));
        String content = title.replace(CONTENT_SPLIT,HtmlTemplateUtil.processContents(paramDataList, dataList));
        return content;
    }

    private static String processTitles(List<String> titles) {
        StringBuilder sb = new StringBuilder(1000);
        for (String title : titles) {
            sb.append("<th>")
                    .append(title)
                    .append("</th>");
        }
        return sb.toString();
    }

    private static String processContents(List<String> paramDataList, List<Map<String, String>> dataList) {
        StringBuilder sb = new StringBuilder();
        if(dataList!=null && dataList.size()>0){
            for (int i = 0;i<dataList.size();i++) {
                sb.append("<tr>");
                sb.append("<td>").append(i+1).append("</td>");
                for (String param : paramDataList) {
                    sb.append("<td>")
                            .append(HtmlTemplateUtil.parseString(dataList.get(i).get(param), ""))
                            .append("</td>");
                }
                sb.append("</tr>");
            }
        }

        return sb.toString();
    }

    private static String parseString(String s,String defaultValule){
        if(StringUtils.isBlank(s)){
            return defaultValule;
        }
        return s;
    }
}
